package inits

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"log"
	"message/appconfig"
	"message/glodal"
	"message/model"
)

var err error

func InitMysql() {
	data := appconfig.ConfigData.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", data.User, data.Pass, data.Host, data.Post, data.Data)
	glodal.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic("数据库连接失败")
	} else {
		log.Println("数据库连接成功")
	}
	err = glodal.DB.AutoMigrate(&model.User{}, &model.Announcement{})
	if err != nil {
		panic("数据库迁移失败")
	} else {
		log.Println("数据库迁移成功")
	}
}
